package com.zhxh.nzpos.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.zhxh.nzpos.db.DBHelper;
import com.zhxh.nzpos.model.Category;
import com.zhxh.nzpos.utils.RandomStringUtils;

/**
 * Category DAO
 * 
 * @author Administrator
 * 
 */
public class CategoryDao {

	DBHelper dbHelper;

	private final static String LOG_TAG = "CategoryDao";
	protected static final String TBL_NAME = "category";

	public CategoryDao(Context context) {
		dbHelper = new DBHelper(context);
	}

	/**
	 * add new category
	 * 
	 * @param category
	 */
	public String save(Category category) {
		Log.i(LOG_TAG, "CategoryDao  save " + category.toString());
		//category.setId(RandomStringUtils.randomNumeric(10));
		long time = System.currentTimeMillis();
		category.setCreated(time);
		category.setModified(time);

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();// 开始事务
		try {
			db.execSQL(
					"insert into category(ID,category,created,modified) values(?,?,?,?)",
					new Object[] { category.getId(), category.getCategory(),
							category.getCreated(), category.getModified() });

			db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.i("CategoryDao  save", e.getMessage());
		}
		db.endTransaction();// 事务结束;
		dbHelper.close();
		return category.getId();
	}

	/**
	 * update a category
	 * 
	 * @param category
	 */

	public void update(Category category) {
		Log.i(LOG_TAG, "CategoryDao  update " + category.toString());
		category.setModified(System.currentTimeMillis());
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();// 开始事务
		try {
			db.execSQL(
					"update category set category=?,modified=? where ID=?",
					new Object[] { category.getCategory(),
							category.getModified(), category.getId() });

			// db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.i("CategoryDao  update", e.getMessage());
		}
		db.endTransaction();// 事务结束;
		dbHelper.close();
	}

	/**
	 * get a category by id
	 * 
	 * @param i
	 * @return
	 */
	public Category find(String id) {
		Log.i(LOG_TAG, "CategoryDao  find " + id);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select ID,category,created,modified from category where ID=?",
				new String[] { id });
		if (cursor.moveToNext()) {
			Category category = new Category();

			category.setId(cursor.getString(cursor.getColumnIndex("ID")));
			category.setCategory(cursor.getString(1));
			category.setCreated(cursor.getLong(2));
			category.setModified(cursor.getLong(3));
			return category;
		}
		cursor.close();
		dbHelper.close();
		return null;
	}

	/**
	 * delete a category by id
	 * 
	 * @param id
	 */
	public void delete(String id) {
		Log.i(LOG_TAG, "CategoryDao  delete " + id);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("delete from category where ID=?", new Object[] { id });
		dbHelper.close();
	}

	/**
	 * delete all
	 * 
	 * @param id
	 */
	public void deleteAll() {
		Log.i(LOG_TAG, "CategoryDao  delete all from  category: ");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("delete from category");
		dbHelper.close();
	}

	/**
	 * fetch categorys
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public List<Category> getScrollData(int pageNumber, int pageSize) {

		Log.i(LOG_TAG, "CategoryDao  getScrollData " + pageNumber + ","
				+ pageSize);

		List<Category> categorys = new ArrayList<Category>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select ID,category,created,modified from category order by modified desc limit ?,?",
				new String[] { String.valueOf((pageNumber - 1) * pageSize),
						String.valueOf(pageSize) });// pageNumber页码
		while (cursor.moveToNext()) {// pageSize每页获取的记录数
			Log.i("aa", "333333333333");
			Category category = new Category();
			category.setId(cursor.getString(cursor.getColumnIndex("ID")));
			category.setCategory(cursor.getString(1));
			category.setCreated(cursor.getLong(2));
			category.setModified(cursor.getLong(3));
			categorys.add(category);
		}
		cursor.close();
		dbHelper.close();
		return categorys;
	}

	/**
	 * fetch all categorys
	 * 
	 * @return
	 */
	public List<Category> getAll() {

		Log.i(LOG_TAG, "CategoryDao  get All data ");

		List<Category> categorys = new ArrayList<Category>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select ID,category,created,modified from category  order by modified desc", null);// pageNumber页码
		while (cursor.moveToNext()) {// pageSize每页获取的记录数
			Log.i("aa", "333333333333");
			Category category = new Category();
			category.setId(cursor.getString(cursor.getColumnIndex("ID")));
			category.setCategory(cursor.getString(1));
			category.setCreated(cursor.getLong(2));
			category.setModified(cursor.getLong(3));
			categorys.add(category);
		}
		cursor.close();
		dbHelper.close();
		return categorys;
	}

	/**
	 * get category's count
	 * 
	 * @return
	 */
	public long getCount() {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		long count = DatabaseUtils.queryNumEntries(db, TBL_NAME);
		dbHelper.close();
		return count;
	}

}
